﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interface.Api;
using System.Net;
using yafc.DataPool;

namespace yafc.SessionLayer
{
	public class Account
	{
		#region Fields

		private IFactory factory;

		private string username;
		private string password;

		private bool logged = false;

		private PostPool myTL = new PostPool();
		private PostPool frndTL = new PostPool();

		#endregion

		#region Public Properties

		public bool Logged
		{
			get { return logged; }
		}

		public string Password
		{
			get { return decrypt(password); }
		}

		public string Username
		{
			get { return username; }
		}

		public ICollection<Post> NewFrndPosts
		{
			get
			{
				return frndTL.NewPosts;
			}
		}

		public ICollection<Post> OldFrndPosts
		{
			get
			{
				return frndTL.OldPosts;
			}
		}

		public ICollection<Post> FrndPosts
		{
			get
				{ return frndTL.AllPosts; }
		}

		#endregion

		#region Contructor

		public Account(IFactory Factory, string Uname, string Passwd)
		{
			factory = Factory;
			username = Uname;
			password = Passwd;
		}

		#endregion

		#region Public Methods

		public bool Equals(Account Acc)
		{
			if (factory == Acc.factory 
				&& Username == Acc.Username)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		#region Oprations

		public bool login()
		{
			if (Username.Length == 0 || Password.Length == 0)
			{
				return false;
			}

			IApi loginApi = factory.create(ApiType.Login);
			LoginResults results = (LoginResults)loginApi.execute(new LoginArgs(Username, Password));

			if (results.status == HttpStatusCode.OK)
				logged = true;
			else
				logged = false;

			return logged;
		}

		public void logout(string User)
		{
			password = "";
		}

		public void post(string Msg)
		{
			if (!Logged)
			{
				throw new Exception("You must log in first.");
			}

			IApi api = factory.create(ApiType.PostMsg);
			PostMsgResults results = (PostMsgResults)api.execute(new PostMsgArgs(Username, Password, Msg));
		}

		public void follow(string Name)
		{

		}

		public void unfollow(string Name)
		{

		}

		public void block(string Name)
		{

		}

		public void updateMyTL()
		{
			if (!Logged)
			{
				throw new Exception("You must log in first.");
			}

			IApi timelineApi = factory.create(ApiType.MyTL);
			TLResults results = (TLResults)timelineApi.execute(new TLArgs(Username, null));

			if (results.status == HttpStatusCode.OK)
			{
				myTL.addPost(results.posts);
			}
		}

		public void updateFrndTL()
		{
			if (!Logged)
			{
				throw new Exception("You must log in first.");
			}

			IApi timelineApi = factory.create(ApiType.FrndTL);
			TLResults results = (TLResults)timelineApi.execute(new TLArgs(Username, Password));

			if (results.status == HttpStatusCode.OK)
			{
				frndTL.addPost(results.posts);
			}
		}

		#endregion

		#endregion

		#region Protected Methods

		protected string encrypt(string S)
		{
			return S;
		}

		protected string decrypt(string S)
		{
			return S;
		}

		#endregion
	}
}
